package cn.maihe.elg.operation.centers.hymake.handler.service;

import cn.maihe.elg.operation.centers.hymake.config.HyMakeInfoConfig;
import cn.maihe.elg.operation.centers.hymake.dto.HyMakeBaseRespDTO;
import cn.maihe.elg.operation.centers.hymake.dto.handler.HyMakeClaimApplyReqDTO;
import cn.maihe.elg.operation.centers.hymake.dto.handler.HyMakeCorrectApplyReqDTO;
import cn.maihe.elg.operation.centers.hymake.handler.HyMakeCenterTradeService;
import cn.maihe.elg.operation.centers.supports.CenterTradeContext;
import cn.maihe.elg.operation.centers.supports.annotation.CenterTradeService;
import cn.maihe.elg.operation.model.enums.*;
import cn.maihe.elg.operation.model.vo.order.AcceptOrderStatusAuditVO;
import cn.maihe.elg.operation.repository.entity.*;
import cn.maihe.elg.operation.service.business.InstitutionService;
import cn.maihe.elg.operation.service.business.ProjectInfoService;
import cn.maihe.elg.operation.service.guarantee.GuaranteeService;
import cn.maihe.elg.operation.service.order.AcceptOrderInfoService;
import cn.maihe.elg.operation.service.order.ApplyOrderInfoService;
import cn.maihe.elg.operation.service.order.RefundOrderInfoService;
import cn.maihe.elg.operation.utils.DateUtil;
import cn.maihe.elg.operation.utils.ElgLogger;
import cn.maihe.elg.operation.utils.IdGenerate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * @Description 保函理赔申请
 * @Author caixh
 * @Date 2022/3/30.
 */
@CenterTradeService(
        provider = CenterProviderEnum.HYMAKE,
        centerNo = CenterNoEnum.NINGDE,
        tradeType = TradeType.lstCorrect,
        reqClass = HyMakeCorrectApplyReqDTO.class,
        respClass = HyMakeBaseRespDTO.class
)
@Slf4j
public class HyMakeCorrectApplyService extends HyMakeCenterTradeService<HyMakeCorrectApplyReqDTO, HyMakeBaseRespDTO> {

    @Resource
    ApplyOrderInfoService applyOrderInfoService;
    @Resource
    AcceptOrderInfoService acceptOrderInfoService;
    @Resource
    RefundOrderInfoService refundOrderInfoService;
    @Resource
    ProjectInfoService projectInfoService;
    @Resource
    private InstitutionService institutionService;
    @Resource
    GuaranteeService guaranteeService;

    public HyMakeCorrectApplyService(HyMakeInfoConfig centerInfosConfig) {
        super(centerInfosConfig);
    }


    /**
     * 具体业务处理
     *
     * @param context
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public HyMakeBaseRespDTO processService(CenterTradeContext<HyMakeCorrectApplyReqDTO, HyMakeBaseRespDTO> context) {

        HyMakeCorrectApplyReqDTO req = context.getReq();
        Long centerId = context.getCenterId();
        Long orgId = context.getOrgId();
        String revokeNo = req.getPolicyNo();

        AcceptOrderInfoDO orderInfo = acceptOrderInfoService.getByGuaranteeNo(centerId, orgId, revokeNo);
        if (orderInfo == null) {
            return HyMakeBaseRespDTO.error("查无此投保申请记录");
        }

        GuaranteeDO guaranteeDO = guaranteeService.getGuaranteeByGuaranteeNo(orderInfo.getGuaranteeNo());
        if (guaranteeDO == null) {
            return HyMakeBaseRespDTO.error("查无此保函记录");
        }

        if (orderInfo.getStatus() == AcceptOrderStatus.FINISH.getStatus()) {
            return HyMakeBaseRespDTO.error("保函已完成，不能进行批改");
        }
        if (orderInfo.getStatus() == AcceptOrderStatus.CANCEL.getStatus()) {
            return HyMakeBaseRespDTO.error("保函已取消，不能进行批改");
        }
        if (orderInfo.getStatus() != AcceptOrderStatus.HAS_OUT_GUARANTEE.getStatus()) {
            return HyMakeBaseRespDTO.error("已出函状态才能发起批改");
        }
        boolean result = acceptOrderInfoService.correctGuarantee(orderInfo,guaranteeDO,req);
        if (!result){
            return HyMakeBaseRespDTO.error("重新出函失败");
        }
        return HyMakeBaseRespDTO.success("批改已受理,请耐心等待审核结果通知");
    }
}
